home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / programm.ing / egem_200.lzh / EGEM.2_0 / SOURCE / XACC_AV.C < prev   
Encoding:
C/C++ Source or Header  |  1994-09-07  |  9.6 KB  |  561 lines

  1.  
  2. #include "proto.h"
  3. #include <string.h>
  4. #include <time.h>
  5.  
  6. int AvServer=-1,_xacc_msgs;
  7. static char *XAccName,*AVName,a_name[9];
  8. static int av_msgs,va_msgs,multi_xacc=FALSE;
  9. static XAcc Xaccs[MAX_XACCS];
  10.  
  11. static void    store_xacc_id(int*);
  12. static void    store_av_id(int*);
  13. static void    kill_id(int,int);
  14.  
  15. static void    XAccSendId(int);
  16. static void    XAccSendAcc(int);
  17. static void XAccSendAccClose(void);
  18. static int XAccWait(int);
  19.  
  20. static void AvProtocol(int);
  21.  
  22. int appl_search(int ap_smode,char *ap_sname,int *ap_stype,int *ap_sid)
  23. {
  24.     INTIN[0] = ap_smode;
  25.     _GemParBlk.addrin[0] = ap_sname;
  26.     _aes(0,0x12010301L);
  27.  
  28.     *ap_stype = INTOUT[1];
  29.     *ap_sid = INTOUT[2];
  30.  
  31.     return (INTOUT[0]);
  32. }
  33.  
  34. int AvSendMsg(int sendto,int msg_id,int *msg)
  35. {
  36.     if (sendto>=0)
  37.     {
  38.         int err;
  39.  
  40.         msg[0] = msg_id;
  41.         msg[1] = ap_id;
  42.         msg[2] = 0;
  43.  
  44.         err = appl_write(sendto,16,msg);
  45.  
  46.         if (err>0)
  47.             return (TRUE);
  48.         else if (err<0 && sendto!=ap_id)
  49.             kill_id(AV|XACC,sendto);
  50.     }
  51.  
  52.     return (FALSE);
  53. }
  54.  
  55. static int XAccSend(int sendto,int msg0,int msg3,char *msg4,int msg6,int msg7)
  56. {
  57.     if (sendto>=0 && sendto!=ap_id)
  58.     {
  59.         int msg[8],err;
  60.  
  61.         msg[0] = msg0;
  62.         msg[1] = ap_id;
  63.         msg[2] = 0;
  64.         msg[3] = msg3;
  65.         *(char **) &msg[4] = msg4;
  66.         msg[6] = msg6;
  67.         msg[7] = msg7;
  68.  
  69.         err = appl_write(sendto, 16, msg);
  70.  
  71.         if (err>0)
  72.             return (TRUE);
  73.         else if (err<0)
  74.             kill_id(AV|XACC,sendto);
  75.     }
  76.  
  77.     return (FALSE);
  78. }
  79.  
  80. int XAccSendAck(int sendto, int answer)
  81. {
  82.     return (XAccSend(sendto,ACC_ACK,(answer) ? 1 : 0,NULL,0,0));
  83. }
  84.  
  85. int XAccSendText(int sendto, char *text)
  86. {
  87.     if (XAccSend(sendto, ACC_TEXT, 0, text, 0, 0)==TRUE)
  88.         return (XAccWait(2000));
  89.     else
  90.         return (FALSE);
  91. }
  92.  
  93. int XAccSendKey(int sendto, int scan, int state)
  94. {
  95.     if (XAccSend(sendto, ACC_KEY, scan, (char *) (((long) state)<<16), 0, 0)==TRUE)
  96.         return (TRUE);
  97.     else
  98.         return (FALSE);
  99. }
  100.  
  101. int XAccSendMeta(int sendto, int last, char *data, long len)
  102. {
  103.     if (XAccSend(sendto, ACC_META, (last) ? 1: 0, data, (int) (len>>16), (int) len)==TRUE)
  104.         return (XAccWait(5000));
  105.     else
  106.         return (FALSE);
  107. }
  108.  
  109. int XAccSendImg(int sendto, int last, char *data, long len)
  110. {
  111.     if (XAccSend(sendto, ACC_IMG, (last) ? 1: 0, data, (int) (len>>16), (int) len)==TRUE)
  112.         return (XAccWait(5000));
  113.     else
  114.         return (FALSE);
  115. }
  116.  
  117. static void XAccSendId(int sendto)
  118. {
  119.     XAccSend(sendto, ACC_ID, (XACCVERSION<<8)|XACC_LEVEL, XAccName, menu_id, 0);
  120. }
  121.  
  122. static void XAccSendAcc(int sendto)
  123. {
  124.     XAccSend(sendto, ACC_ACC, (XACCVERSION<<8)|XACC_LEVEL, XAccName, menu_id, ap_id);
  125. }
  126.  
  127. void _XAccAvExit(void)
  128. {
  129.     reg XAcc *xacc;
  130.     reg int i;
  131.     int msg[8];
  132.  
  133.     if (multi_xacc)
  134.     {
  135.         msg[3] = ap_id;
  136.  
  137.         for (i=0,xacc=Xaccs;i<MAX_XACCS;xacc++,i++)
  138.         {
  139.             if (xacc->flag & AV)
  140.                 AvSendMsg(xacc->id,AV_EXIT,msg);
  141.  
  142.             if (xacc->flag & XACC)
  143.                 AvSendMsg(xacc->id,ACC_EXIT,msg);
  144.         }
  145.     }
  146. }
  147.  
  148. static void XAccInformAcc(int *msg)
  149. {
  150.     reg XAcc *xacc;
  151.     reg char *name = *((char **) &msg[4]);
  152.     reg int i;
  153.  
  154.     for (i=0,xacc=Xaccs;i<MAX_XACCS;xacc++,i++)
  155.         if (xacc->flag & XACC)
  156.             XAccSend(xacc->id,ACC_ACC,msg[3],name,msg[6],msg[1]);
  157. }
  158.  
  159. static void XAccSendAccClose(void)
  160. {
  161.     if (!_app && !multi_xacc)
  162.     {
  163.         kill_id(AV|XACC,0);
  164.         AvServer = -1;
  165.         XAccSendId(0);
  166.         AvProtocol(0);
  167.     }
  168. }
  169.  
  170. int AppLoaded(char *app)
  171. {
  172.     reg char buf[9], *p, *q;
  173.     reg int i,id;
  174.  
  175.     if (app==NULL)
  176.         return (-1);
  177.  
  178.     p = q = app;
  179.     while ((q=strpbrk(p,":\\/"))!=NULL)
  180.     {
  181.         q++;
  182.         p = q;
  183.     }
  184.  
  185.     for (i=0;i<8;i++)
  186.         if(*p>' ' && *p!='.')
  187.             buf[i] = *p++;
  188.         else
  189.             buf[i] = ' ';
  190.     buf[8] = '\0';
  191.  
  192.     id = appl_find(buf);
  193.     if (id==ap_id)
  194.         id = -1;
  195.  
  196.     return (id);
  197. }
  198.  
  199. void XAccBroadCast(int *msg)
  200. {
  201.     reg XAcc *xacc;
  202.     reg char name[10];
  203.     reg int i;
  204.     int next,type,id;
  205.     int dummy,search;
  206.  
  207.     if (aes_version>=0x0400 || magx || (appl_xgetinfo(4,&dummy,&dummy,&search,&dummy) && search>0))
  208.     {
  209.         next = 0;
  210.         while (appl_search(next, name, &type, &id))
  211.         {
  212.             if (id!=ap_id && (type & 0x06))
  213.                 AvSendMsg(id,msg[0],msg);
  214.             next = 1;
  215.         }
  216.     }
  217.     else
  218.         for (i=0,xacc=Xaccs;i<MAX_XACCS;xacc++,i++)
  219.             if (xacc->id>=0)
  220.                 AvSendMsg(xacc->id,msg[0],msg);
  221. }
  222.  
  223. void _XAccSendStartup(char *xacc_name, char *av_name, int Avmsgs,int Vamsgs, int xacc_msgs)
  224. {
  225.     reg char name[10], *buf, *p;
  226.     int xacc_size,av_size;
  227.     int i,next,type,id,dummy,search;
  228.  
  229.     XAccName = p = xacc_name;
  230.  
  231.     while (*p++);
  232.     if (!stricmp(p,"XDSC"))
  233.     {
  234.         do
  235.         {
  236.             while (*p++);
  237.         } while (*p);
  238.     }
  239.  
  240.     xacc_size = (int) (p - xacc_name);
  241.  
  242.     strcpy(a_name,"        ");
  243.     AVName = memcpy(a_name,av_name,min((int) strlen(av_name),8));
  244.     av_size = 8+1;
  245.  
  246.     av_msgs = Avmsgs;
  247.     va_msgs = Vamsgs|MSG_EXIT|MSG_SENDKEY|MSG_ACCWINDOPEN;
  248.     _xacc_msgs = xacc_msgs;
  249.  
  250.     for (i=0;i<MAX_XACCS;i++)
  251.         Xaccs[i].id = -1;
  252.  
  253.     if (mint)
  254.     {
  255.         buf = Mxalloc(xacc_size+av_size, 0x0023);
  256.         if (buf!=NULL)
  257.         {
  258.             XAccName = memcpy(buf, XAccName, xacc_size);
  259.             buf += xacc_size;
  260.             AVName = memcpy(buf, AVName, av_size);
  261.         }
  262.     }
  263.  
  264.     if (aes_version>=0x0400 || magx || (appl_xgetinfo(4,&dummy,&dummy,&search,&dummy) && search>0))
  265.     {
  266.         multi_xacc = TRUE;
  267.  
  268.         next = 0;
  269.         while (appl_search(next, name, &type, &id))
  270.         {
  271.             if (id!=ap_id && (type & 0x06))
  272.                 XAccSendId(id);
  273.             next = 1;
  274.         }
  275.     }
  276.  
  277.     id = AppLoaded(getenv("AVSERVER"));
  278.     if (id<0)
  279.         id = appl_find("AVSERVER");
  280.     if (id<0)
  281.         id = appl_find("GEMINI  ");
  282.     if (id<0)
  283.         id = appl_find("VENUS   ");
  284.  
  285.     if (id==ap_id)
  286.         AvServer = -1;
  287.     else
  288.         AvServer = id;
  289.  
  290.     if (!_app & !multi_xacc)
  291.         XAccSendAccClose();
  292.     else if (AvServer>=0)
  293.         AvProtocol(AvServer);
  294. }
  295.  
  296. static int XAccWait(int wait)
  297. {
  298.     XEVENT event;
  299.     reg int *msg = event.ev_mmgpbuf;
  300.  
  301.     event.ev_mflags = MU_MESAG|MU_TIMER;
  302.     event.ev_mthicount = 0;
  303.     event.ev_mtlocount = wait;
  304.     event.ev_mtlast = 0;
  305.  
  306.     while (Event_Multi(&event)==MU_MESAG)
  307.     {
  308.         switch (msg[0])
  309.         {
  310.         case ACC_ACK:
  311.             return((msg[3]==1) ? TRUE : FALSE);
  312.         case AC_OPEN:
  313.         case AC_CLOSE:
  314.             if (_app)
  315.                 break;
  316.         case AP_TERM:
  317.                 return (FALSE);
  318.         }
  319.     }
  320.  
  321.     return(FALSE);
  322. }
  323.  
  324. static void AvProtocol(int id)
  325. {
  326.     int msg[8];
  327.  
  328.     if (id!=ap_id)
  329.     {
  330.         msg[3] = av_msgs;
  331.         msg[4] = msg[5] = 0;
  332.         *(const char **) &msg[6] = AVName;
  333.         AvSendMsg(id,AV_PROTOKOLL,msg);
  334.     }
  335. }
  336.  
  337. static void AvProtoStatus(int id)
  338. {
  339.     int msg[8];
  340.  
  341.     if (id!=AvServer && id!=ap_id)
  342.     {
  343.         msg[3] = va_msgs;
  344.         msg[4] = msg[5] = 0;
  345.         *(const char **) &msg[6] = AVName;
  346.         AvSendMsg(id,VA_PROTOSTATUS,msg);
  347.     }
  348. }
  349.  
  350. int _XAccComm(int *msg)
  351. {
  352.     switch (msg[0])
  353.     {
  354.     case AC_CLOSE:
  355.         XAccSendAccClose();
  356.         return (FALSE);
  357.     case ACC_ACC:
  358.         if (multi_xacc)
  359.         {
  360.             if (msg[7]<=0 || msg[1]==msg[7])
  361.             {
  362.                 AvProtoStatus(msg[1]);
  363.                 store_xacc_id(msg);
  364.             }
  365.         }
  366.         else if (!_app)
  367.         {
  368.             msg[1] = msg[7];
  369.             XAccSendId(msg[1]);
  370.             store_xacc_id(msg);
  371.         }
  372.         break;
  373.     case ACC_ID:
  374.         if (multi_xacc)
  375.         {
  376.             XAccSendAcc(msg[1]);
  377.             AvProtoStatus(msg[1]);
  378.         }
  379.         else if (_app)
  380.         {
  381.             XAccInformAcc(msg);
  382.             XAccSendId(msg[1]);
  383.         }
  384.         store_xacc_id(msg);
  385.         break;
  386.     case AV_EXIT:
  387.         kill_id(AV|XACC,msg[3]);
  388.         break;
  389.     case ACC_EXIT:
  390.         kill_id(XACC,msg[1]);
  391.         break;
  392.     case AV_PROTOKOLL:
  393.         AvProtoStatus(msg[1]);
  394.     case VA_PROTOSTATUS:
  395.         store_av_id(msg);
  396.         break;
  397.     default:
  398.         return (FALSE);
  399.     }
  400.  
  401.     return (TRUE);
  402. }
  403.  
  404. static void store_av_id(int *msg)
  405. {
  406.     reg XAcc *xacc=Xaccs;
  407.     reg int id=msg[1],i;
  408.  
  409.     if (id==ap_id || id<0)
  410.         return;
  411.  
  412.     if (id==0 && !_app && !multi_xacc)
  413.         AvServer = 0;
  414.  
  415.     for (i=0;i<MAX_XACCS && xacc->id>=0;xacc++,i++)
  416.         if (xacc->id==id)
  417.             break;;
  418.  
  419.     if (i<MAX_XACCS)
  420.     {
  421.         xacc->flag |= AV;
  422.  
  423.         xacc->id = id;
  424.         strncpy(xacc->name,*((char **) &msg[6]),8);
  425.  
  426.         _send_msg(xacc,id,XACC_AV_INIT);
  427.     }
  428. }
  429.  
  430. static void store_xacc_id(int *msg)
  431. {
  432.     reg XAcc *xacc;
  433.     reg int id=msg[1],i;
  434.     reg char *xdsc;
  435.  
  436.     if (id==ap_id || id<0)
  437.         return;
  438.  
  439.     for (i=0,xacc=Xaccs;i<MAX_XACCS && xacc->id>=0;xacc++,i++)
  440.         if (xacc->id==id)
  441.             break;
  442.  
  443.     if (i<MAX_XACCS)
  444.     {
  445.         xacc->flag |= XACC;
  446.  
  447.         xacc->id = id;
  448.         xacc->version = msg[3];
  449.         xacc->menu_id = msg[6];
  450.         xacc->xname = *((char **) &msg[4]);
  451.         xacc->xdsc = NULL;
  452.         if (xacc->xname!=NULL)
  453.         {
  454.             xdsc = xacc->xname + strlen(xacc->xname)+1;
  455.             if (!strcmp(xdsc,"XDSC"))
  456.                 xacc->xdsc = xdsc+5;
  457.         }
  458.  
  459.         _send_msg(xacc,id,XACC_AV_INIT);
  460.     }
  461. }
  462.  
  463. static void kill_id(int flag,int id)
  464. {
  465.     reg XAcc *xacc;
  466.     reg int i;
  467.  
  468.     if (id<0)
  469.         return;
  470.  
  471.     for (i=0,xacc=Xaccs;i<MAX_XACCS;xacc++,i++)
  472.         if (id==xacc->id)
  473.         {
  474.             xacc->flag &= ~flag;
  475.             if (id==AvServer && (xacc->flag & AV)==0)
  476.                 AvServer = -1;
  477.  
  478.             if (xacc->flag==0)
  479.             {
  480.                 memset(xacc,0,sizeof(XAcc));
  481.                 xacc->id = -1;
  482.             }
  483.             else if (flag==AV)
  484.                 xacc->name[0] = '\0';
  485.             else
  486.             {
  487.                 xacc->xname = xacc->xdsc = NULL;
  488.                 xacc->version = xacc->menu_id = 0;
  489.             }
  490.  
  491.             _send_msg(xacc,id,XACC_AV_EXIT);
  492.             return;
  493.         }
  494. }
  495.  
  496. XAcc *find_xacc_xdsc(int id,char *dsc)
  497. {
  498.     reg XAcc *xacc;
  499.     reg char *xdsc;
  500.     reg int i;
  501.  
  502.     for (i=0,xacc=Xaccs;i<MAX_XACCS;xacc++,i++)
  503.         if (xacc->id>=0 && (id<0 || xacc->id==id))
  504.         {
  505.             if ((xdsc=xacc->xdsc)!=NULL)
  506.             {
  507.                 while (*xdsc!='\0')
  508.                 {
  509.                     if (!strcmp(xdsc,dsc))
  510.                         return (xacc);
  511.                     xdsc += strlen(xdsc)+1;
  512.                 }
  513.             }
  514.  
  515.             if (xacc->id==id)
  516.                 break;
  517.         }
  518.  
  519.     return (NULL);
  520. }
  521.  
  522. static int next=-1;
  523.  
  524. XAcc *find_app(int first)
  525. {
  526.     reg XAcc *xacc;
  527.     reg int i;
  528.  
  529.     if (first)
  530.         i = 0;
  531.     else if (next<0 || next>=MAX_XACCS)
  532.         return (NULL);
  533.     else
  534.         i = next;
  535.  
  536.     for (xacc=&Xaccs[i];i<MAX_XACCS;xacc++,i++)
  537.         if (xacc->id>=0)
  538.         {
  539.             next = i+1;
  540.             return (xacc);
  541.         }
  542.  
  543.     next = -1;
  544.     return (NULL);
  545. }
  546.  
  547. XAcc *find_id(int id)
  548. {
  549.     reg XAcc *xacc;
  550.     reg int i;
  551.  
  552.     if (id<0)
  553.         return (NULL);
  554.  
  555.     for (i=0,xacc=Xaccs;i<MAX_XACCS;xacc++,i++)
  556.         if (xacc->id==id)
  557.             return (xacc);
  558.  
  559.     return (NULL);
  560. }
  561.